export default {
    data() {
        return {
            question_id: 0,
            ruleForm: {
                question: "",
                answer: "",
                sort: '',
                radio:'1'
            },
            rules: {
                question: [
                    {required: true, message: '请输入问题描述', trigger: 'blur'},
                ],
                answer: [
                    {required: true, message: '请输入问题答案', trigger: 'blur'},
                ],
            },
        }
    },
    //进入页面加载
    mounted: function () {
        let that = this;
        //在缓存中获取值
        that.user_name = sessionStorage.getItem("user_name");
        that.user_id = sessionStorage.getItem("user_id");
        that.token = sessionStorage.getItem("access-token");
        if ((!that.token) || (!that.user_id) || (!that.user_name)) {
            that.$router.push('/login');
        }
        //加载轮播详情
        if (that.$route.query.question_id) {
            that.question_id = that.$route.query.question_id;
            that.detail();
        }
    },

    methods: {

        /**
         * 返回
         */
        cancelQuestions:function () {
            let that = this;
            that.$router.push({path: '/user/question'});
        },

        /**
         * 保存
         * @param formName
         */
        submitForm(formName) {
            let that = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (that.question_id === 0) {
                        that.addMemberGrade();
                    } else {
                        that.editMemberGrade();
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },

        /**
         * 添加问题
         * @returns {boolean}
         */
        addMemberGrade: function () {
            var that = this;
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.user_id = that.user_id;
            formData.question = that.ruleForm.question;
            formData.answer = that.ruleForm.answer;
            formData.sort = that.ruleForm.sort;
            formData.status = that.ruleForm.radio;

            //请求api
            that.$http.post(that.adminApi.api_url + "/Question/add", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    that.$message({
                        type: 'success',
                        message: `操作提示: ${ '添加成功' }`
                    });
                    that.$router.push({path: '/user/question'});
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 详情
         */
        detail: function () {
            var that = this;
            //请求的数据
            var formData = {};
            formData.token = that.token;
            formData.id = that.question_id;

            //请求邀请者列表
            that.$http.post(that.adminApi.api_url + "/Question/show_edit", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    that.ruleForm.question = res.body.question;
                    that.ruleForm.answer = res.body.answer;
                    that.ruleForm.radio = res.body.status.toString();
                    that.ruleForm.sort = res.body.sort
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

        /**
         * 修改
         * @returns {boolean}
         */
        editMemberGrade: function () {
            //请求的数据
            let that= this;
            let formData = {};
            formData.token = that.token;
            formData.id = that.question_id;
            formData.user_id = that.user_id;
            formData.question = that.ruleForm.question;
            formData.answer = that.ruleForm.answer;
            formData.sort = that.ruleForm.sort;
            formData.status = that.ruleForm.radio;
            //请求API
            that.$http.post(that.adminApi.api_url + "/Question/edit", formData, {
                emulateJSON: true
            }).then(
                function (res) {
                    // 处理成功的结果
                    that.$message({
                        type: 'success',
                        message: `操作提示: ${ '修改成功' }`
                    });
                    that.$router.push({path: '/user/question'});
                },
                function () {
                    // 处理失败的结果
                    that.$message({
                        type: 'error',
                        message: `操作提示: ${ '处理异常' }`
                    });
                });
        },

    }
}